package common;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;



import org.h2.util.New;

import common.cache.CacheManager;
import common.rssReader.RssReader;

import play.Play;
import play.cache.Cache;
import play.cache.CacheImpl;
import play.jobs.Job;
import play.jobs.OnApplicationStart;

@OnApplicationStart
public class Bootstrap extends Job {

	@Override
	public void doJob() {
	    
		final String calaisAPIKey = Play.configuration.getProperty("open.calais.api.key");
		CacheManager.setJavaCacheItem("calaisAPIKey", calaisAPIKey);
	 

		// TODO Auto-generated method stub
		String url = "http://feeds.bbci.co.uk/news/rss.xml";
		String url2 = "http://rss.cnn.com/rss/cnn_world.rss";
		String url3 = "http://feeds.nytimes.com/nyt/rss/HomePage";
		String url4 = "http://rss.msnbc.msn.com/id/3032506/device/rss/rss.xml";

		RssReader rssReader = RssReader.getInstance();
		rssReader.addRssUrl(url);
		rssReader.addRssUrl(url4);
		rssReader.addRssUrl(url2);
		rssReader.addRssUrl(url3);
		

		ArrayList<String> selectorNYT = new ArrayList<String>();
		selectorNYT.add("div.articleBody > p");
		selectorNYT.add("div.entry-content > p");
		CacheManager.setJavaCacheItem("www.nytimes.com", selectorNYT);
		CacheManager.setJavaCacheItem("feeds.nytimes.com", selectorNYT);
		
		ArrayList<String> selectorNYTBlog = new ArrayList();
		selectorNYTBlog.add("div.entry-content > p");
		CacheManager.setJavaCacheItem("http://lens.blogs.nytimes.com", selectorNYTBlog);
		
		ArrayList<String> selectorBBC = new ArrayList<String>();
		selectorBBC.add("div.story-body > p");
		selectorBBC.add("div#meta-information > p");
		selectorBBC.add("div.story-body > div.article > p");
		CacheManager.setJavaCacheItem("www.bbc.co.uk", selectorBBC);
		
		ArrayList<String> selectorMSNBC = new ArrayList<String>();
		selectorMSNBC.add("div.articleText > p");
		selectorMSNBC.add("div.intelliTXT >p");
		
		CacheManager.setJavaCacheItem("worldnews.msnbc.msn.com", selectorMSNBC);
		CacheManager.setJavaCacheItem("photoblog.msnbc.msn.com",selectorMSNBC);
		CacheManager.setJavaCacheItem("usnews.msnbc.msn.com", selectorMSNBC); 
		CacheManager.setJavaCacheItem("www.msnbc.msn.com",selectorMSNBC);
		
		ArrayList<String> selectorCNN = new ArrayList<String>();
		selectorCNN.add("div.cnn_strycntntlft > p");
		CacheManager.setJavaCacheItem("rss.cnn.com", selectorCNN);

//		if (host.equals(NYTBlog)) {
//			cleanArticle = articleWithJunk.select("div.entry-content > p");
//
//		}
//		else if (host.equals(BBC)) {
//			cleanArticle = articleWithJunk.select("div.story-body > p");
//
//			if (cleanArticle.isEmpty()) {
//				cleanArticle = articleWithJunk
//						.select("div#meta-information > p");
//			}
//
//		}
		if (RssReader.lastReadedTime == null) {
			RssReader.lastReadedTime = DateUtils.getNow();
			RssReader.lastReadedTime.setMinutes(RssReader.lastReadedTime.getMinutes() - 10);
		}
		new NewsFetcher().now();

	}
}
